package com.colorful.insuranceproject.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.BankCardAdapter;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.BankCardBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MineRequest;
import com.lovely3x.common.utils.Response;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 银行卡列表
 */
public class BankCardListActivity extends TitleActivity implements View.OnClickListener {

    private static final int GET_BANK_CARD_LIST_URL = 0X1;

    private static final int DELETE_BANK_CARD_URL = 0X2;

    private static final int REQUEST_CODE = 10000;

    private MineRequest mineRequest;

    private BankCardAdapter bankCardAdapter;

    @Bind(R.id.lv_bank_card)
    ListView lvBankCard;

    @Bind(R.id.tv_add_bank_card)
    TextView addOrDelete;

    private TextView manager;

    /**
     * 是否是在编辑模式
     */
    private boolean mIsEditMode;

    @Override
    protected int getContentView() {
        return R.layout.activity_bank_card_list;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.bank_card_list));

        addRightBtn(getString(R.string.manager), R.id.manager);
        manager = (TextView) findViewById(R.id.manager);
        manager.setOnClickListener(this);
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
        bankCardAdapter = new BankCardAdapter(null, mActivity);
        lvBankCard.setAdapter(bankCardAdapter);
        mineRequest.getBankCardList(GET_BANK_CARD_LIST_URL);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_BANK_CARD_LIST_URL:
                if (response.isSuccessful) {
                    setPageData((List<BankCardBean>) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            case DELETE_BANK_CARD_URL:
                if (response.isSuccessful) {
                    showToast(getString(R.string.delete_bank_card_success));
                    bankCardAdapter.clear();
                    dismissProgressCircle();
                    mineRequest.getBankCardList(GET_BANK_CARD_LIST_URL);
                    enterEditMode(false);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    private void setPageData(List<BankCardBean> obj) {
        bankCardAdapter.setData(obj);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            mineRequest.getBankCardList(GET_BANK_CARD_LIST_URL);
        }
    }

    @OnClick(R.id.tv_add_bank_card)
    void OnClicked() {
        if (mIsEditMode) {//　删除状态
            String listDelete = "";
            for (BankCardBean bean : bankCardAdapter.getDatas()) {
                if (bean.getJudgeType() == 1) {
                    listDelete += bean.getBankCarNumber() + ",";
                }
            }
            if (listDelete.equals("")) {
                showToast(getString(R.string.please_choose_bank_card));
            } else {
                final String list = listDelete;
                new AlertDialog.Builder(mActivity)
                        .setTitle(getString(R.string.hint))
                        .setMessage(getString(R.string.confrim_delete_card))
                        .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                showProgressCircle();
                                mineRequest.deleteBankCard(list, DELETE_BANK_CARD_URL);
                            }
                        }).setNegativeButton(getString(R.string.cancel),null).show();
            }
        } else {
            launchActivityForResult(AddBankCardActivity.class, REQUEST_CODE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.manager:
                if (manager.getText().equals(getString(R.string.manager)))
                    enterEditMode(true);
                else
                    enterEditMode(false);
                break;
        }
    }


    /**
     * 进入或退出编辑模式
     *
     * @param enter 是否进入编辑模式
     */
    protected void enterEditMode(boolean enter) {
        if (mIsEditMode != enter) {
            this.mIsEditMode = enter;
            if (enter) {//如果是进入编辑模式
                manager.setText(getString(R.string.cancel));
                addOrDelete.setText(getString(R.string.delete_bank_card));
                for (BankCardBean bean : bankCardAdapter.getDatas()) {
                    bean.setVisiableCheckBox(1);
                }
                bankCardAdapter.notifyDataSetChanged();
            } else {//退出编辑模式
                manager.setText(getString(R.string.manager));
                for (BankCardBean bean : bankCardAdapter.getDatas()) {
                    bean.setVisiableCheckBox(0);
                }
                bankCardAdapter.notifyDataSetChanged();
                addOrDelete.setText(getString(R.string.add_bank_card));
            }
        }
    }
}
